package com.axjy.comm.config;

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceConfig {

    @Resource
    private DataSourceProperties dataSourceProperties;

    @Bean
    @RefreshScope
    public DataSource dataSource(){
        DataSourceBuilder builder=DataSourceBuilder.create();
        builder.url(dataSourceProperties.getUrl());
        builder.driverClassName(dataSourceProperties.getDriverClassName());
        builder.username(dataSourceProperties.getUsername());
        builder.password(dataSourceProperties.getPassword());
        return builder.build();
    }
}